package org.dromara.common.core.utils;

import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description  学校适用的一些工具类
 * @Date 2023/11/3 13:55
 * @Author 郭建波
 **/
public class SchoolUtils {

    public static Map<Integer,String> findSchoolGrade(int schoolYear){
        TreeSet<Integer> findInSchoolYear = findInSchoolYear(schoolYear);
        Map<Integer,String> map =  new TreeMap<>(Collections.reverseOrder());
        AtomicInteger i = new AtomicInteger(schoolYear);
        for(Integer y :findInSchoolYear) {
            int j = i.get();
            if(j>0) {
                map.put(y, changeNumToZh(j)+"年级");
                i.decrementAndGet();
            }
        }
        return map;
    }


    public static Map<String,Integer> findSchoolYearByGrade(int schoolYear){

        TreeSet<Integer> findInSchoolYear = findInSchoolYear(schoolYear);
        Map<String,Integer> map = new HashMap<String,Integer>();
        AtomicInteger i = new AtomicInteger(schoolYear);


        for(Integer y :findInSchoolYear) {
            int j = i.get();
            if(j>0) {

                map.put(changeNumToZh(j)+"年级",y);
                i.decrementAndGet();
            }
        }
        return map;
    }



    public static String changeNumToZh(int num) {
        switch (num) {
            case 1:
                return "一";
            case 2:
                return "二";
            case 3:
                return "三";
            case 4:
                return "四";
            case 5:
                return "五";
            case 6:
                return "六";
            case 7:
                return "七";
            case 8:
                return "八";
            case 9:
                return "九";
            default:
                return "";
        }

    }


    /**
     *
     * {2019=五年级, 2020=四年级, 2021=三年级, 2022=二年级, 2023=一年级}
     * @param type 学校性质 幼儿园 小学 初中 。。。
     * @return
     */
  public static  Map<Integer, String> getSchoolClassYear(String type){

     return findSchoolGrade(getYearByType(type));
  }

    /**
     * {一年级=2023, 五年级=2019, 三年级=2021, 二年级=2022, 四年级=2020}
     * @param type 学校性质 幼儿园 小学 初中 。。。
     * @return
     */
    public static  Map<String, Integer> getSchoolClassName(String type){

        return findSchoolYearByGrade(getYearByType(type));
    }



    public static int getYearByType(String type) {
        switch (type) {
            case "幼儿园":
            case "职校 ":
            case "高中":
                return 3;
            case "小学":
                return 5;
            case "初中":
            case "大学":
                return 4;
            case "九年制":
                return 9;
            case "其他":
                return 1;
            default:
                return 0;
        }

    }





    /**
     * 截取4位数字 转换成对应map，并返回类似一年级 二年级
     * @return
     */
    public static String chenageYearNumbertoZHNumber(int clazzYear,int clazzNum,int schoolYear) {
        Map<Integer, String> findSchoolGrade = findSchoolGrade(schoolYear);
        String s = findSchoolGrade.get(clazzYear);
        return s+clazzNum+"班";
    }



    /*获取目前所有在校班级的年份
//     *
//     */
    public static TreeSet<Integer> findInSchoolYear(int schoolYear){
//        List<Integer> list = new ArrayList<Integer>();
        TreeSet<Integer> list = new TreeSet<>();
        int year = getYear();
        for(int i=schoolYear-1;i>=0;i--){
            list.add(year-i);
        }
        return list;
    }


    public static int getYear(){
        Calendar cal = Calendar.getInstance();
        int year=cal.get(Calendar.YEAR);
        int month=cal.get(Calendar.MONTH)+1;
        if(month<7){
            //获取到的月份如果<9则表明没有开学，只能获取上学期的数据
            year=year-1;
        }
        return year;
    }

    /**
     *
     * @param className 一年级1班
     * @return  一年级
     */
    public static String getclass(String className){
        String clazz = className.substring(0,3);
        return clazz;
    }

    /**
     *
     * @param className 一年级1班
     * @return  1
     */
    public static Integer getclassNum(String className){
        Integer num = null;
        try{
            String clazzNum = className.substring(3,className.length() - 1);
            num =Integer.valueOf(clazzNum);
        }catch(Exception e){
            throw  new RuntimeException("请输入正确的班级号，请用数字！");
        }

        return num;
    }





    public static void main(String[] args) {
        Map<Integer, String> schoolGrade = findSchoolGrade(5);
        System.out.println(schoolGrade);
//        Map<String, Integer> schoolYearByGrade = findSchoolYearByGrade(5);
//        System.out.println(schoolYearByGrade);
//        System.out.println(aa);
        String sex="女";

//        Integer rsex = sex=="男"?0:sex=="女"?1


//        Map<String, Integer> schoolClassName = SchoolUtils.getSchoolClassName(SchoolType.SYSTEM_OF_NINEYEAR.getType());
//        Integer getyear = schoolClassName.get("一年级");
//        System.out.println(getyear);

//        Integer aa = getclassNum("一年级11班");:2;
//        System.out.println(rsex);

    }




}
